<template>
	<view class="accountEditWeChat">
		<template v-if="associationStatus">
			<view class="prompt">您已绑定微信，点击以下按钮进行解绑。</view>
			<view class="button" style="background: #eb3331;" @click="clickSecure">
				<text class="iconfont icon-weixin"></text>解除绑定
			</view>
		</template>
		<template v-if="!associationStatus">
			<view class="prompt">您还没有绑定微信哟，点击以下按钮进行绑定。</view>
			<view class="button" @click="clickAssociation">
				<text class="iconfont icon-weixin"></text>绑定微信
			</view>
		</template>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				associationStatus:false
			}
		},
		onLoad() {
			this.get();
		},
		methods: {
			get(){
				this.$_Request(
					"GET","/api/v2/account/detail",{},
					(res) => {
						// 感谢网友 Nahida 贡献以下代码
						// 有问题联系微信 Naruto1986519
						res.data.data.detail.connects.forEach((item, i) => {
							if (item.connectPlatformId == 25) {
								this.associationStatus = item.connected;
								return;
							}
						})
					}
				)
			},
			// 解除绑定
			clickSecure(){
				uni.showLoading();
				this.$_Request(
					"PUT","/api/v2/account/edit",
					{
						"disconnectPlatformId":25
					},
					(res) => {
						uni.hideLoading()
						if(res.data.code == 0){
							uni.navigateBack();
						}else{
							uni.showToast({
								title: res.data.message,
								icon:'none'
							});
						}
					}
				)
			},
			// 绑定微信
			clickAssociation(){
				uni.showLoading();
				uni.login({
					provider:'weixin',
					success: (res) => {
						this.weixinCode = res.code;
						this.postWeChatLoginOne();
					}
				})
			},
			// 微信登录方法，第一次登录
			postWeChatLoginOne(){
				this.$_Request(
					"POST","/api/wechat-login/mini-program/oauth",
					{
						"code":this.weixinCode,
					},
					(res) => {
						uni.hideLoading()
						if(res.data.code == 0){
							uni.setStorageSync("account",res.data);
							uni.navigateBack();
						}else{
							uni.showToast({
								title: res.data.message,
								icon:'none'
							});
						}
					}
				)
			},
		}
	}
</script>

<style lang="scss">
.accountEditWeChat{
	.prompt{
		font-size: 28rpx;
		padding: 25rpx;
		color: $ZhanWeiSe;
	}
	.button{
		background: #43be4a;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 25rpx 25rpx 25rpx;
		line-height: 32rpx;
		padding: 50rpx 0;
		border-radius: 10rpx;
		color: #FFF;
		text{
			font-size: 45rpx;
			color: #FFF;
			padding-right: 10rpx;
		}
	}
}
</style>
